<meta charset="utf-8">
<?php

class ListmusicController extends Controller
{
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
	public function getList5($idView,$idUser='')
	{
		if($idUser=='')
			$relation = 'me';
		else
			$relation = (Friendship::model()->isFriend($idUser,$idView) ? 'friend' : 'none');

		$lists = Listmusic::model()->getList5($idView,$relation);
		$data  = array();
		foreach ($lists as $list)
			$data[] = $list->attributes;
		return $data;
	}

	public function getContentFeed($idView,$idUser='',$page=1)
	{
		$limit = ($page - 1)*5;
		if($idUser=='')
			$relation = 'me';
		else
			$relation = (Friendship::model()->isFriend($idUser,$idView) ? 'friend' : 'none');

		$where = '';
		switch ($relation)
		{
			case 'friend':
			$where = ' AND pricavy != "private" ';
				break;
			case 'none':
			$where = ' AND pricavy = "public" ';
				break;
		}

		/*$sql='
			SELECT * FROM (
				SELECT * FROM music WHERE feed="1" AND user_id= '.$idView.$where.'
				UNION
				SELECT * FROM listmusic WHERE feed="1" AND user_id= '.$idView.$where.'
			) AS joinTable ORDER BY joinTable.posttime DESC LIMIT '.$limit.',5
		';*/

		$sql = '
			SELECT * FROM (
				SELECT * FROM music WHERE feed="1" AND user_id= '.$idView.$where.'
			UNION
				SELECT * FROM listmusic WHERE feed="1" AND user_id= '.$idView.$where.'
			UNION
				SELECT
					tag.id,music.user_id,music.title,music.posttime,music.link,music.content,music.`like`,tag.feed,music.pricavy,music.other
				FROM tag INNER JOIN music ON music.id = tag.music_id
				WHERE tag.feed = 1 AND tag.user_id = '.$idView.$where.'
			UNION
				SELECT
					tag.id,listmusic.user_id,listmusic.title,listmusic.posttime,listmusic.content,listmusic.photo,listmusic.`like`,tag.feed,listmusic.pricavy,listmusic.other
				FROM tag INNER JOIN listmusic ON listmusic.id = tag.list_id
				WHERE tag.feed = 1 AND tag.user_id = '.$idView.$where.'
			) AS joinTable ORDER BY joinTable.posttime DESC LIMIT '.$limit.',5
		';

		$query=Yii::app()->db->createCommand($sql)->queryAll();

		for ($i=0; $i < count($query); $i++)
		{
			if(substr($query[$i]['link'], 0,4) == 'http'){
				$query[$i]['type'] = 'music';
				$query[$i]['link'] = substr($query[$i]['link'],-11);
			}
			else
			{
				$query[$i]['type']    = 'list';
				$query[$i]['photo']   = $query[$i]['content'];
				$query[$i]['content'] = $query[$i]['link'];
				$query[$i]['link']    = Listmusic::model()->viewMusic($query[$i]['id']);
				for ($j=0; $j < count($query[$i]['link']) ; $j++) {
					$query[$i]['link'][$j]['link'] = substr($query[$i]['link'][$j]['link'],-11);
				}
			}

			$query[$i]['like'] = array(
				'quantity' => $query[$i]['like'],
				'data' => LikeController::viewLike($query[$i]['id'],$query[$i]['type'])
			);

			//Thêm nội dung vắn tắt người thích
			if(count($query[$i]['like']['data'])<4)
			{
				$contentLike = '';
				$end = count($query[$i]['like']['data']);

				for ($j=1; $j < $end; $j++)
				{
					$contentLike .= '<a href="'.Yii::app()->request->baseUrl.'/Page/Feed/'.$query[$i]['like']['data'][$j]['id'].'"><strong>';
					$contentLike .= $query[$i]['like']['data'][$j]['name'].'</strong></a>, ';
				}

				$contentLike .= '<a href="'.Yii::app()->request->baseUrl.'/Page/Feed/'.$query[$i]['like']['data'][$end]['id'].'"><strong>';
				$contentLike .= $query[$i]['like']['data'][$end]['name'].'</strong></a> đã thích bài viết này.';
			}
			else
			{
				$idEnd = count($query[$i]['like']['data']);
				$contentLike = '<a href="'.Yii::app()->request->baseUrl.'/Page/Feed/'.$query[$i]['like']['data'][$idEnd-1]['id'].'"><strong>';
				$contentLike .= $query[$i]['like']['data'][$idEnd-1]['name'].'</strong></a> và <a href="javascript:void(0)" class="btnOverplay"><strong>'.($idEnd-1).' người bạn khác</a></strong> đã thích bài viết này.';
			}


			$query[$i]['comment'] = CommentController::viewComment($query[$i]['id'],$query[$i]['type']);

			$query[$i]['user'] = User::model()->viewIdNameAvatar($query[$i]['user_id']);
			$query[$i]['contentLike'] = $contentLike;
			$query[$i]['posttime']= NotificationController::getNotificationDateTime($query[$i]['posttime']);
		}
		return $query;
	}

	public function getContentNew($user_id,$page=1)
	{
		$limit = ($page - 1)*5;
		$sql = '
		SELECT * FROM (
				SELECT * FROM music WHERE user_id= '.$user_id.'
			UNION
				SELECT * FROM music WHERE pricavy = "public" AND user_id not in(SELECT friendship.friend_id FROM friendship WHERE friendship.user_id = '.$user_id.' AND status = "friend")
			UNION
				SELECT * FROM music WHERE pricavy != "private" AND user_id in(SELECT friendship.friend_id FROM friendship WHERE friendship.user_id = '.$user_id.' AND status = "friend")
			UNION
				SELECT * FROM listmusic WHERE user_id= '.$user_id.'
			UNION
				SELECT * FROM listmusic WHERE pricavy = "public" AND user_id not in(SELECT friendship.friend_id FROM friendship WHERE friendship.user_id = '.$user_id.' AND status = "friend")
			UNION
				SELECT * FROM music WHERE pricavy != "private" AND user_id in(SELECT friendship.friend_id FROM friendship WHERE friendship.user_id = '.$user_id.' AND status = "friend")
			) AS joinTable ORDER BY joinTable.posttime DESC LIMIT '.$limit.',5
		';
		$query=Yii::app()->db->createCommand($sql)->queryAll();

		for ($i=0; $i < count($query); $i++)
		{
			if(substr($query[$i]['link'], 0,4) == 'http'){
				$query[$i]['type'] = 'music';
				$query[$i]['link'] = substr($query[$i]['link'],-11);
			}else{
				$query[$i]['type']    = 'list';
				$query[$i]['photo']   = $query[$i]['content'];
				$query[$i]['content'] = $query[$i]['link'];
				$query[$i]['link']    = $data = Listmusic::model()->viewMusic($query[$i]['id']);;
				for ($j=0; $j < count($query[$i]['link']) ; $j++) {
					$query[$i]['link'][$j]['link'] = substr($query[$i]['link'][$j]['link'],-11);
				}
			}
			$query[$i]['like'] = array(
				'quantity' => $query[$i]['like'],
				'data' => LikeController::viewLike($query[$i]['id'],$query[$i]['type'])
			);

			//Thêm nội dung vắn tắt người thích
			/*if(count($query[$i]['like']['data'])<4)
			{
				$contentLike = '';
				$end = count($query[$i]['like']['data']);

				for ($j=1; $j < $end; $j++)
				{
					$contentLike .= '<a href="'.Yii::app()->request->baseUrl.'/Page/Feed/'.$query[$i]['like']['data'][$j]['id'].'"><strong>';
					$contentLike .= $query[$i]['like']['data'][$j]['name'].'</strong></a>, ';
				}

				$contentLike .= '<a href="'.Yii::app()->request->baseUrl.'/Page/Feed/'.$query[$i]['like']['data'][$end]['id'].'"><strong>';
				$contentLike .= $query[$i]['like']['data'][$end]['name'].'</strong></a> đã thích bài viết này.';
			}
			else
			{
				$idEnd = count($query[$i]['like']['data']);
				$contentLike = '<a href="'.Yii::app()->request->baseUrl.'/Page/Feed/'.$query[$i]['like']['data'][$idEnd-1]['id'].'"><strong>';
				$contentLike .= $query[$i]['like']['data'][$idEnd-1]['name'].'</strong></a> và <a href="javascript:void(0)" class="btnOverplay"><strong>'.($idEnd-1).' người bạn khác</a></strong> đã thích bài viết này.';
			}*/
			$contentLike = count($query[$i]['like']['data']) . ' người đã thích bài viết này.';

			$query[$i]['comment'] = CommentController::viewComment($query[$i]['id'],$query[$i]['type']);

			$query[$i]['user'] = User::model()->viewIdNameAvatar($query[$i]['user_id']);
			$query[$i]['contentLike'] = $contentLike;
			$query[$i]['posttime']= NotificationController::getNotificationDateTime($query[$i]['posttime']);
		}
		return $query;
	}

	public function getListRandom()
	{
		$data = Listmusic::model()->getListRandom();

		if(isset($data[0]))
		{
			$listRandom = array();
			for ($i=0; $i < count($data); $i++)
			{
				$listRandom[] = $data[$i]->attributes;
			}
			return $listRandom;
		}
	}

	public function getListFavorite()
	{
		$data = Listmusic::model()->getListFavorite();

		if(isset($data[0]))
		{
			$listFavorite = array();
			for ($i=0; $i < count($data); $i++)
			{
				$listFavorite[] = $data[$i]->attributes;
			}
			return $listFavorite;
		}
	}

	public function actionTest()
	{
		$test = $this->getContentNew(1);
		var_dump($test);
	}
}